package com.jie.servlet;

import com.jie.entity.User;
import com.jie.service.UserService;
import com.jie.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by 12904 on 2018/8/17.
 */
@WebServlet(name = "loginServlet")
public class loginServlet extends HttpServlet
{
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        String uname = request.getParameter("username");
        String password = request.getParameter("password");
        User user = new User();
        user.setUname(request.getParameter("username"));
        user.setUpassword(request.getParameter("password"));



        UserService service = new UserServiceImpl();
        if (service.login(uname,password) > 0){
            HttpSession session = request.getSession();
            session.setAttribute("user", user);
            out.print(service.login(uname,password));
        }
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
         doPost(request,response);

    }
}
